/**
 * Created by TOPFEEL on 2018/2/24.
 */

layui.use(['layer', 'table', 'form'], function () {
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;

    var $=layui.$;

    //表单重新渲染
    form.render();

    //表格渲染
    var tableObj = table.render({
        id: 'list_table'
        , elem: '#list_table'
        , url: 'sys/org/getListByPid'
        , align: "center"
        , cols: [[ //表头
             {type:'numbers',title: '序号', width: 50}
            , {field: 'orgName', title: '机构名称', width: 140, align: 'center'}
            , {field: 'orgOrder', title: '排序', width: 70, align: 'center'}
            , {field: 'isLeafName', title: '叶子节点', width: 90, align: 'center'}
            , {
                field: 'orgState', title: '状态', width: 70, align: 'center', templet: function (d) {
                    return d.orgState == 1 ? '<span class="font-success">启用</span>' : '<span class="font-danger">禁用</span>';
                }
            }
            , {field: 'updateTime', title: '修改时间', width: 170, align: 'center'}
            , {field: 'lock', title: '操作', width: 80, templet: function (d) {
                return d.isSystem=='1'?'<button class="layui-btn layui-btn-sm layui-btn-disabled" disabled lay-event="edit">修改</button>':
                    '<button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="edit">修改</button>';
            }, unresize: true, align: 'center'}
        ]]
    });

    //监听工具条
    table.on('tool(mainList)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data //获得当前行数据
            , layEvent = obj.event; //获得 lay-event 对应的值

        if (layEvent === 'edit') {

            showDialog({
                title: '修改'
                , template: 'org-edit'
                , saveUrl: 'sys/org/save'
                , tableObj: tableObj
                , width: 500
                , height: 400
                , htmlData: data
                ,submited:function () {

                    //获取左侧ztree
                    getZtree();
                }
            })
        }
    });


    //查询数据
    $('#query').on('click', function () {
        reloadTable(tableObj);
    });



    // 顶部 [添加]和[删除]操作事件
    $("#org_add").click(function () {
        var nodes=ztreeObj.getSelectedNodes();
        if(nodes.length == 0){
            layer.msg("请选择节点");
            return;
        }
        var node = nodes[0];

        showDialog({
            title: '添加'
            , template: 'org-add'
            , saveUrl: 'sys/org/save'
            , tableObj: tableObj
            , width: 500
            , height: 400
            , htmlData: {"parentNo":$("#list_form input[name='orgNo']").val()}
            ,afterSubmit:function () {
                getZtree();
            }
        });


    });

    //修改
    $("#org_edit").click(function () {
        var nodes=ztreeObj.getSelectedNodes();
        if(nodes.length==0){
            layer.msg("请选择节点");
            return;
        }
        var node=nodes[0];
        if(node.level==0){
            layer.msg("根节点不可修改");
            return;
        }

        $.ajax({
            url: 'sys/org/getByOrgNo?orgNo='+node.id,
            contentType: 'application/json',
            success: function (result) {
                if(result && result.data){
                    showDialog({
                        title: '修改'
                        , template: 'edit'
                        , saveUrl: '/sys/org/save'
                        , tableObj: tableObj
                        , width: 500
                        , height: 400
                        , htmlData: result.data
                        ,afterSubmit:function () {
                            //获取左侧ztree
                            getZtree();
                        }
                    });
                }
            },
            error: function () {
                layer.msg("操作异常");
            }
        });


    });

    $("#org_delete").click(function () {
        var nodes=ztreeObj.getSelectedNodes();
        if(nodes.length==0){
            layer.msg("请选择节点");
            return;
        }
        var node=nodes[0];
        if(node.level==0){
            layer.msg("根节点不可删除");
            return;
        }
        if(node.isParent){
            layer.msg("请先删除子节点");
            return;
        }
        var obj = {"orgNo": node.id};
        showConfirm('确认删除？',function () {
            $.ajax({
                type: "GET",
                url: 'sys/org/delete',
                data: obj,
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg || "删除成功");
                        reloadTable(tableObj);
                        getZtree();
                    }else{
                        layer.msg( result.msg || "删除异常");
                    }
                },
                error: function () {
                    layer.msg("删除异常");
                }
            })
        },0);
    });


    var ztreeObj = null;

    var setting = {
        data: {
            key: {
                title: "t"
            },
            simpleData: {
                enable: true
            }
        },
        view: {
            showIcon: true,
            showTitle: false
        },
        callback: {
            onClick: ztreeCallback
        }
    };


    //获取左侧ztree
    getZtree();

    function getZtree() {
        $.ajax({
            url: 'sys/org/list',
            success: function (result) {
                var zNodes = result;
                ztreeObj=initZtree("ztree", setting, zNodes);
            },
            error: function () {
                layer.msg("操作异常");
            }
        });
    }



    function ztreeCallback(event, treeId, treeNode) {
        var nodeId = treeNode.id;
        $("#list_form input[name='orgNo']").val(nodeId);
        reloadTable(tableObj);
    }

});